/*                                                                         
 * Copyright 2010-2013 the original author or authors.                     
 *                                                                         
 * Licensed under the Apache License, Version 2.0 (the "License");         
 * you may not use this file except in compliance with the License.        
 * You may obtain a copy of the License at                                 
 *                                                                         
 *      http://www.apache.org/licenses/LICENSE-2.0                         
 *                                                                         
 * Unless required by applicable law or agreed to in writing, software     
 * distributed under the License is distributed on an "AS IS" BASIS,       
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and     
 * limitations under the License.                                          
 */
package com.shopng.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author sgao20
 *
 */
public class LogUtil
{
  private static Logger coreLog;
  
  static
  {
    coreLog = LoggerFactory.getLogger("com.shopng");
  }
  
  public static Logger getCoreLog()
  {
    return coreLog;
  }
  
 
  private final static Watch localWatch=new Watch();
  public final static void startTimer()
  {
    localWatch.rest();
  }
  
  public final static long cost()
  {
    return localWatch.cost();
  }
  
  public long longCost()
  {
    return localWatch.longCost();
  }
  
  public final static Watch newWatch()
  {
    return new Watch();
  }
  
  
  public static class Watch
  {
    private final ThreadLocal<Long> TL_COSTUSE = new ThreadLocal<>();
    private final ThreadLocal<Long> TL_START = new ThreadLocal<>();
    
    public Watch()
    {
      
    }
    public void rest()
    {
      long l = System.currentTimeMillis();
      TL_COSTUSE.set(l);
      TL_START.set(l);
    }
    
    public long cost()
    {
      Long end = TL_COSTUSE.get();
      long t1 = end != null ? end.longValue() : System.currentTimeMillis();
      long t2 = System.currentTimeMillis();
      long d = t2 - t1;
      startTimer();
      return d;
    }
    
    public long longCost()
    {
      Long end = TL_START.get();
      long t1 = end != null ? end.longValue() : System.currentTimeMillis();
      long t2 = System.currentTimeMillis();
      long d = t2 - t1;
      startTimer();
      return d;
    }
  }
  
}
